package com.labichaoka.chaoka.ui.baseinfo.person;

import android.text.TextUtils;

import com.labichaoka.chaoka.base.MyApplication;
import com.labichaoka.chaoka.entity.BaseResponse;
import com.labichaoka.chaoka.entity.FaceAuthRequest;
import com.labichaoka.chaoka.entity.FaceAuthResponse;
import com.labichaoka.chaoka.entity.ProvinceDataPersistModel;
import com.labichaoka.chaoka.entity.SaveInformationRequest;
import com.labichaoka.chaoka.utils.FileUtil;
import com.labichaoka.chaoka.utils.ThreadPoolManager;
import com.labichaoka.chaoka.utils.WrappedGson;

/**
 * Created by ruan on 2018/8/7.
 */

public class PersonInfoPresenterImpl implements PersonInfoPresenter,PersonInfoInteractor.OnFinishedListener{
    private PersonInfoInteractor infoInteractor;
    private PersonInfoView view;

    public PersonInfoPresenterImpl(PersonInfoView view,PersonInfoInteractor personInfoInteractor){
        this.view=view;
        this.infoInteractor=personInfoInteractor;
    }

    @Override
    public void saveInformation(SaveInformationRequest request) {
        if (view!=null){
            view.showProgress();
        }
        infoInteractor.saveInformation(request,this);
    }

    @Override
    public void faceAuth(FaceAuthRequest request) {
        if (view!=null){
            view.showProgress();
        }
        infoInteractor.faceAuth(request,this);
    }

    @Override
    public void behavior(String actionNode) {
        infoInteractor.behavior(actionNode);
    }

    @Override
    public void getFaceAuthStatus() {
        if (view!=null){
            view.showProgress();
        }
        infoInteractor.getFaceAuthStatus(this);
    }

    @Override
    public void onDestroy() {
        view=null;
    }

    @Override
    public void onSaveSuccessed(BaseResponse response) {
        view.hideProgress();
        view.toNext();
    }

    @Override
    public void onSaveFailed() {
        view.hideProgress();
    }

    @Override
    public void onFaceAuthSucc(FaceAuthResponse response) {
        view.hideProgress();
        view.faceAuthCallback(response);
    }

    @Override
    public void onFaceAuthFailed() {
        view.hideProgress();
    }

    @Override
    public void onGetFaceAuthStatusSucc(FaceAuthResponse response) {
        view.hideProgress();
        view.getFaceAuthStatus(response);
    }

    @Override
    public void onGetFaceAuthStatusFailed() {
        view.hideProgress();
    }

    @Override
    public void initProvinceData() {
        ThreadPoolManager.getInstance().execute(() -> {
            try{
                String json = null;
                json = FileUtil.readStringFromFileDir("province.dat", null, false);
                json = (json == null) ?
                        FileUtil.readStringFromInputStream(MyApplication.mContext.getAssets().open("province.dat"), null)
                        : json;
                if (!TextUtils.isEmpty(json)){
                    final ProvinceDataPersistModel provData =
                            WrappedGson.getInstance().fromJson(json, ProvinceDataPersistModel.class);
                    view.initProvince(provData);
                }
            }catch (Exception e){
                e.printStackTrace();
            }
        });
    }
}
